const api = require("../api.js");
//获取应用实例
const {network} = getApp();

Page({
  data: {
    url: ''
  },
  onLoad(e) {
    console.log(e, '进来的参数');
    const params = decodeURIComponent(e.scene);
    console.log(params, 'params参数');
    let index = params.indexOf('pages');
    let url = params.slice(index, params.length);
    this.setData({
      url: '?' + params
    });
    setTimeout(() => {
      wx.redirectTo({url: '/pages/home/main/index'})
    }, 3000);
  },
  onShow() {
    // wx.showLoading();
    network.post(api.ScanningQRCode, {
      PageUrl:  this.data.url,
      ScanningIdentity: 'consumer' // ['promoters' => 促销员, 'merchants' => 商家, 'consumer' =》 客户]
    }).then((data) => {
      let success = false;
      let title = '';
      const result = data.QRCodeReturnString.toLocaleLowerCase();
      switch(result) {
        case 'qrcodeerror':
          title = '不支持的二维码';
          break;
        case 'alreadybound':
          title = '绑定失败，已绑定过';
          break;
        case 'bindsuccess':
          title = '绑定成功';
          success = true;
          break;
      }
      wx.showToast({
        title: title,
        icon: 'none'
      });
      if (success) {
        wx.navigateToMiniProgram({
          appId: data.AppId,
          path: `/pages/home/main/index?u=${data.UserPkId}`
        });
      }
      console.log(data, "客户");
    }, (e) => {
      console.log(e, 'error');
      wx.showToast({
        title: 'err',
        icon: 'none'
      });
    })
  }
});
